package com.clippick;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import co.kr.clippick.R;

import com.clippick.api.GetApi;
import com.clippick.common.Toasts;
import com.clippick.common.Utils;

public class NewForgotActivity extends Activity {
	private Context mContext;
	private ConnectivityManager manager;

	private EditText emailEditText;
	private Button resetButton, backButton;
	private LinearLayout backButtonLayer;

	private String userEmail = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_forgot);
		mContext = this;

		manager = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		emailEditText = (EditText) findViewById(R.id.email);
		resetButton = (Button) findViewById(R.id.reset);
		backButton = (Button) findViewById(R.id.back);
		backButtonLayer = (LinearLayout) findViewById(R.id.back_button_layer);

		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		backButtonLayer.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		resetButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				userEmail = emailEditText.getText().toString();
				if (!Utils.checkEmail(userEmail)) {
					if (Utils.checkNetwokState(manager)) {
						new SendTask().execute();
					} else {
						Toasts.showToast(mContext,
								getString(R.string.network_fail));
					}
				} else {
					Toasts.showToast(mContext,
							getString(R.string.signup_wrong_email));
				}
			}
		});

		emailEditText
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {
					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_GO) {
							userEmail = emailEditText.getText().toString();
							if (!Utils.checkEmail(userEmail)) {
								if (Utils.checkNetwokState(manager)) {
									new SendTask().execute();
								} else {
									Toasts.showToast(mContext,
											getString(R.string.network_fail));
								}
							} else {
								Toasts.showToast(mContext,
										getString(R.string.signup_wrong_email));
							}
						}

						return true;
					}
				});
	}

	private void hideKeyboard(EditText editText) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}

	class SendTask extends AsyncTask<String, String, String> {
		ProgressDialog mProgress = null;
		String response = null;

		protected void onPreExecute() {
			hideKeyboard(emailEditText);

			mProgress = new ProgressDialog(mContext);
			mProgress.setMessage("Loading...");
			mProgress.setCancelable(false);
			mProgress.show();

			resetButton.setEnabled(false);
		}

		@Override
		protected String doInBackground(String... params) {
			GetApi api = new GetApi(mContext);
			response = api.forgotPasswordApi(userEmail);

			return null;
		}

		protected void onPostExecute(String unused) {
			mProgress.dismiss();
			resetButton.setEnabled(true);

			if (response.equals("success")) {
				Toasts.showToast(mContext, getString(R.string.forgot_success));
				finish();
			} else {
				Toasts.showToast(mContext, getString(R.string.forgot_failed));
			}
		}
	}
}
